package com.study.基础知识;

/**
 * sleep方法没有释放锁，而wait方法释放了锁
 */
public class WaitSleepDemo {
    private static Object locker = new Object();
 
    public static void main(String[] args) throws InterruptedException {
        WaitSleepDemo waitDemo = new WaitSleepDemo();
 
        // 启动新线程，防止主线程被休眠
        new Thread(() -> {
            try {
                waitDemo.doWait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        Thread.sleep(100); // 此行本身没有意义，是为了确保 wait() 先执行再执行 notify()
        waitDemo.doNotify();
    }
 
    /**
     * 执行 wait()
     */
    private void doWait() throws InterruptedException {
        synchronized (locker) {
            System.out.println("wait start.");
            locker.wait();
            Thread.sleep(500);
            System.out.println("wait end.");
        }
    }
 
    /**
     * 执行 notify()
     */
    private void doNotify() throws InterruptedException {
        synchronized (locker) {
            System.out.println("notify start.");
            locker.notify();
            System.out.println("notify end.");
            Thread.sleep(1000);
        }
    }
}